package com.entities.projecttask.requestmapparser;

import java.util.Date;

import com.core.utils.RequestParams;
import com.entities.projecttaskpriority.servicelayer.TaskPriorityServiceLayer;
import com.entities.projecttaskresolution.servicelayer.TaskResolutionServiceLayer;
import com.entities.projecttaskstatus.servicelayer.TaskStatusServiceLayer;
import com.entities.projecttasktype.servicelayer.TaskTypeServiceLayer;
import com.entities.projectversion.servicelayer.VersionListLayer;
import com.entities.user.servicelayer.UserListLayer;
import com.model.project.ProjectTask;
import com.model.project.Version;
import com.model.project.attributes.TaskPriority;
import com.model.project.attributes.TaskResolution;
import com.model.project.attributes.TaskStatus;
import com.model.project.attributes.TaskType;
import com.model.user.User;


public class ProjectTaskMapParser {

	public static ProjectTask fromMap ( RequestParams map ) {

		if ( map == null ) {
			return null;
		}

		Integer taskTypeId = map.integerAtIndexForKey(0, "taskType");

		Integer taskPriorityId = map.integerAtIndexForKey(0, "taskPriority");

		Integer taskStatusId = map.integerAtIndexForKey(0, "taskStatus");

		Integer taskResolutionId = map.integerAtIndexForKey(0, "taskResolution");

		Integer reporterId = map.integerAtIndexForKey(0, "reporter");

		Integer assigneeId = map.integerAtIndexForKey(0, "assignee");

		Integer fixVersionId = map.integerAtIndexForKey(0, "fixVersion");

		Integer affectVersionId = map.integerAtIndexForKey(0, "affectVersion");

		String name = map.stringAtIndexForKey(0, "name");

		String description = map.stringAtIndexForKey(0, "description");
		
		Date createdDate = map.dateAtIndexForKeyWithFormat(0, "createDate", "dd.MM.yyyy");

		TaskTypeServiceLayer taskTypeServiceLayer = new TaskTypeServiceLayer();
		TaskType taskType = (TaskType) taskTypeServiceLayer.objectByUNID(taskTypeId, null, null);

		TaskPriorityServiceLayer taskPriorityServiceLayer = new TaskPriorityServiceLayer();
		TaskPriority taskPriority = (TaskPriority) taskPriorityServiceLayer.objectByUNID(taskPriorityId, null, null);

		TaskStatusServiceLayer taskStatusServiceLayer = new TaskStatusServiceLayer();
		TaskStatus taskStatus = (TaskStatus) taskStatusServiceLayer.objectByUNID(taskStatusId, null, null);

		TaskResolutionServiceLayer taskResolutionServiceLayer = new TaskResolutionServiceLayer();
		TaskResolution taskResolution = (TaskResolution) taskResolutionServiceLayer.objectByUNID(taskResolutionId, null, null);

		UserListLayer userServiceLayer = new UserListLayer();
		User taskAssignee = (User) userServiceLayer.objectByUNID(assigneeId, null, null);
		User taskReporter = (User) userServiceLayer.objectByUNID(reporterId, null, null);

		VersionListLayer versionServiceLayer = new VersionListLayer();
		Version fixVersion = (Version) versionServiceLayer.objectByUNID(fixVersionId, null, null);
		Version affectVersion = (Version) versionServiceLayer.objectByUNID(affectVersionId, null, null);

		ProjectTask template = null;
		template = new ProjectTask();
		template.setName(name);
		template.setDescription(description);
		template.setTaskType(taskType);
		template.setTaskPriority(taskPriority);
		template.setTaskStatus(taskStatus);
		template.setTaskResolution(taskResolution);
		template.setAssignee(taskAssignee);
		template.setReporter(taskReporter);
		template.setFixVersion(fixVersion);
		template.setAffectVersion(affectVersion);
		template.setCreateDate(createdDate);

		return template;

	}

}
